//
//  BrandTitleViewController.m
//  HongFuShop
//
//  Created by chirui on 2017/8/3.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "BrandTitleViewController.h"
#import "MyCenterCViewCell.h"
#import "GoodsDetailsViewController.h"

@interface BrandTitleViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    
    UICollectionView * _collectionView;
    NSArray * _dataArray;
}
@end

@implementation BrandTitleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        
    TitleView(self.titleStr);
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_public_return" target:self action:@selector(back)];
    
    [self creatCollectionView];
    [self getHotGoodsRequest];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma creatCollectionView

-(void)creatCollectionView {
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:_collectionView];
    _collectionView.showsVerticalScrollIndicator = NO;
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    
        
                [self getHotGoodsRequest];
        
        
    }];
    _collectionView.mj_header = header;
    
    //注册cell类
    [_collectionView registerNib:[UINib nibWithNibName:@"MyCenterCViewCell" bundle:nil] forCellWithReuseIdentifier:@"MyCenterCViewCell"];
    
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return _dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary * dataDic = _dataArray[indexPath.row];
    MyCenterCViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCenterCViewCell" forIndexPath:indexPath];
    cell.clipsToBounds = NO;
    
    [cell.goodsImage sd_setImageWithURL:ImageUrl(dataDic[@"src"]) placeholderImage:ImageName(@"img_new_default")];
    cell.gooseName.text = dataDic[@"goods_name"];
    cell.scoreLabel.text = [NSString stringWithFormat:@"人气排行：%@",dataDic[@"rank"]];
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

        NSDictionary * dataDic = _dataArray[indexPath.row];
        GoodsDetailsViewController * goods =[[GoodsDetailsViewController alloc]init];
        goods.goodsId = [NSString stringWithFormat:@"%@",dataDic[@"goods_id"]];
        [self.navigationController pushViewController:goods animated:YES];
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //    第一个参数是宽，第二个参数是高 5s  1.8   6 1.6   6p
    float height;
    if (ISP5||ISP4) {
        height = 1.5;
    }else if (ISP6){
        height = 1.3;
        
    }else if (ISP6P){
        height = 1.4;
    }
    
    CGSize size=CGSizeMake((WIDTH-10)/2, (WIDTH-10)/2*height);
    
    return size;
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    //上 左 下 右
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

//设置水平间隙   默认10
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

////设置垂直间隙  默认10
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(0,10);
}
//设置footer的宽度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    //第一个参数：水平滑动时footer的宽度
    //第二个参数：垂直滑动时footer的高度
    return CGSizeMake(0, 0);
}


-(void)getHotGoodsRequest{
    
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",NetWorkAddress_api,get_heardGoods_api];
    NSDictionary * dic = @{@"type":self.typeStr};
    //    SHuiMBhubNoTimeLoding;
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:dic  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            if ([responseObject[@"datas"] isKindOfClass:[NSArray class]]) {
                _dataArray = responseObject[@"datas"];
            }
        } else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        [_collectionView.mj_header endRefreshing];
        [_collectionView reloadData];
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
        [_collectionView.mj_header endRefreshing];
        
    }];
    
    
}



@end
